package org.csu.newpetstoreback.controller.front;

import org.apache.ibatis.annotations.Update;
import org.csu.newpetstoreback.common.CommonResponse;
import org.csu.newpetstoreback.entity.Cart;
import org.csu.newpetstoreback.service.CartService;
import org.csu.newpetstoreback.service.CatalogService;
import org.csu.newpetstoreback.vo.CartItem;
import org.csu.newpetstoreback.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/cart/{username}/")
public class CartController {

    @Autowired
    private CartService cartService;



    @GetMapping("cartItems")
    @ResponseBody
    public CommonResponse<List<CartItem>> getCartItems( @PathVariable("username")String username){
        return cartService.getCartItems(username);
    }

    @DeleteMapping("cartItems")
    @ResponseBody
    public CommonResponse<String> deleteAllCartItems( @PathVariable("username")String username){
        return  cartService.deleteAllCartItems(username);
    }

    @PostMapping("cartItems/{itemId}")
    @ResponseBody
    public CommonResponse<List<CartItem>> addCartItem( @PathVariable ("username")String username, @PathVariable ("itemId")String itemId,@RequestParam int increment){
        return cartService.addCartItem(username,itemId,increment);
    }

    @DeleteMapping("cartItems/{itemId}")
    @ResponseBody
    public CommonResponse<List<CartItem>> removeItemFromCart( @PathVariable ("username")String username, @PathVariable ("itemId")String itemId){
        return cartService.removeItemFromCart(username,itemId);
    }

    @PatchMapping("cartItems/{itemId}")
    @ResponseBody
    public CommonResponse<CartItem> updateCartItemQuantity(@PathVariable ("username")String username, @PathVariable ("itemId")String itemId,@RequestParam(value = "quantity",required = true) int quantity){
        return cartService.updateCartItemQuantity(username,itemId,quantity);
    }








}
